﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;

namespace UofCASE.AgileProductLinerDSL {
    static class ResultHandler {

        private static Test GetTestModel(string relativePath, Store store) {
            foreach (var item in store.ElementDirectory.FindElements<Test>()) {
                if (item.RelativePath.Equals(relativePath)) {
                    return item;
                }
            }
            return null;
        }

        public static void SetTestResults(IList<UofCASE.GreenPepe.GPTest.Test> tests, Store store) {
            using (Transaction t = store.TransactionManager.BeginTransaction("SetTestResults")) {
                foreach (var test in tests) {
                    String relativePath = test.GetRelativePath();
                    Test testModel = GetTestModel(relativePath, store);
                    // in case model can not be found return (not found can also mean wrong diagram)
                    if (testModel == null) return;
                    if (test.TestResult.Successful) testModel.LastTestRun = TestResult.Successful;
                    else testModel.LastTestRun = TestResult.Fail;
                }
                t.Commit();
            }
        }


    }
}
